package liumengbaio.lolzhangyoubao.Activity.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;

import java.io.IOException;

import liumengbaio.lolzhangyoubao.Activity.Adapter.MoreSpecialSecondAdapter;
import liumengbaio.lolzhangyoubao.Activity.InFo.MoreSpecialSecondInfo;
import liumengbaio.lolzhangyoubao.Activity.utils.ScroListView;
import liumengbaio.lolzhangyoubao.R;

/**
 * 精彩专栏二级页面
 * Created by lanouhn on 16/9/18.
 */
public class MoreSpecialSecondActivity extends AppCompatActivity {

    private MoreSpecialSecondInfo info;
    private MoreSpecialSecondAdapter adapter;
    private ScroListView mListView;
    private ImageView pic, back;
    private TextView name;
    private String id;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.more_special_second);
        initView();
        initData();
        initEvent();
        getData();
    }

    private void initView() {
        mListView = (ScroListView) findViewById(R.id.second_listView);
        pic = (ImageView) findViewById(R.id.iv_pic);
        name = (TextView) findViewById(R.id.tv_name);
        back = (ImageView) findViewById(R.id.more_recreation_back);
    }

    private void initData() {
        Intent intent = getIntent();
        String title = intent.getStringExtra("name");
        name.setText(title);
        String icon = intent.getStringExtra("pic");
        Picasso.with(this).load(icon).into(pic);
        id = intent.getStringExtra("topic_id");
    }

    private void initEvent() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MoreSpecialSecondActivity.this, MoreSpecialThirdActivity.class);
                intent.putExtra("id", info.getData().get(position).getId());
                startActivity(intent);
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getData() {
        String url = " http://lol.zhangyoubao.com/apis/rest/TopicsService/items?page=1&topicid=" + id + "&i_=Jfo" +
                "ZvarCyLfJIqs9SRFolapk21/wEw63JVMaX7Y3v5w=&deviceId=JfoZvarCyLfJIqs9SRFolapk21/wEw63JVMa" +
                "X7Y3v5w=&t_=1474166522776&p_=10673&v_=400901&a_=lol&pkg_=com.anzogame.lol&d_=android&osv" +
                "_=19&cha=360Martket&u_=15863305&modle_=MX4&api=TopicsService.items&time=1474166522&game=l" +
                "ol&secretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPhESA&secretVersion=v1.0&nonce=884847&secretSig" +
                "nature=2kgLuneLmK1dWIwaDFSCIpvJTlM= \n";

        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                Toast.makeText(MoreSpecialSecondActivity.this, "连接失败！", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(Response response) throws IOException {
                String result = response.body().string();
                Gson gson = new Gson();
                info = gson.fromJson(result, MoreSpecialSecondInfo.class);
                if (info.getData().size() > 0) {
                    handler.sendEmptyMessage(1);
                } else {
                    handler.sendEmptyMessage(0);
                }
            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(MoreSpecialSecondActivity.this, "数据获取失败！", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    adapter = new MoreSpecialSecondAdapter(MoreSpecialSecondActivity.this, info);
                    mListView.setAdapter(adapter);
                    break;
            }
        }
    };
}
